package com.cy.pj.sys.service.serviceImpl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.sys.dao.RoleDao;
import com.cy.pj.sys.pojo.Role;
import com.cy.pj.sys.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Override
    public PageInfo<Role> selectRole(String name, Integer pageNum, Integer pageSize) {

        //1.调用PageHelper的静态方法开启分页功能
        PageHelper.startPage(pageNum,pageSize);
        //2.执行查询
        List<Role> list = roleDao.selectRole(name);
        //3.封装到PageInfo对象中
        return new PageInfo<>(list);
    }

    @Override
    public int deleteRoleById(Integer[] ids) {
        //判断参数合法性
        if(ids==null ||ids.length==0){
            throw new IllegalArgumentException("请选择一个");
        }
        int row = roleDao.deleteRoleById(ids);
        if(row==0){
            throw new ServiceException("记录可能已经不存在");
        }
        return row;
    }

    @Override
    public int insertRole(Role role) {
        if(role==null) {
            throw new IllegalArgumentException("保存对象不能为空");
        }
        if(role.getName()==null){
            throw new IllegalArgumentException("角色名不能为空");
        }
        int row;
        if(role.getId()==null) {
            row = roleDao.insertRole(role);
            return row;
        }else{
            row=roleDao.updateRoleById(role);
            return  row;
        }
    }

    @Override
    public Role selectRoleById(Integer id) {
        if(id==null){
            throw new ServiceException("请选择一个");
        }
        Role role = roleDao.selectRoleById(id);
        return role;
    }


}
